#include "window.h"

/* 功能实现 */
void select_color(GtkWidget *label);
void select_font(GtkWidget *label);

/* 槽 */
void on_fontToolItem_clicked(GtkWidget *widget, gpointer window);
void on_colorToolItem_clicked(GtkWidget *widget, gpointer window);
void on_fileChooserItem_clicked(GtkWidget *widget, gpointer window);

GtkToolItem* toolbar_add_toolItem(ToolBar *toolbar, const gchar *sock_id);

ToolBar *toolbar_new(gpointer w)
{
    ToolBar *toolbar;
    GtkWidget *bar;

    toolbar = (ToolBar *)g_malloc(sizeof(ToolBar));

    bar = gtk_toolbar_new();
    gtk_toolbar_set_style(GTK_TOOLBAR(bar), GTK_TOOLBAR_ICONS);
    gtk_container_set_border_width(GTK_CONTAINER(bar), 2);

    toolbar->toolbar = bar;
    toolbar->fontToolItem = toolbar_add_toolItem(toolbar, GTK_STOCK_SELECT_FONT);
    toolbar->colorToolItem = toolbar_add_toolItem(toolbar, GTK_STOCK_SELECT_COLOR);
    toolbar->fileChooserItem = toolbar_add_toolItem(toolbar, GTK_STOCK_OPEN);

    /* setting signal */
    g_signal_connect(G_OBJECT(toolbar->colorToolItem), "clicked", G_CALLBACK(on_colorToolItem_clicked), w);
    g_signal_connect(G_OBJECT(toolbar->fontToolItem), "clicked", G_CALLBACK(on_fontToolItem_clicked), w);
    g_signal_connect(G_OBJECT(toolbar->fileChooserItem), "clicked", G_CALLBACK(on_fileChooserItem_clicked), w);

    return toolbar;
}

GtkToolItem* toolbar_add_toolItem(ToolBar *toolbar, const gchar *sock_id)
{
    GtkToolItem *toolItem = gtk_tool_button_new_from_stock(sock_id);
    GTK_WIDGET_UNSET_FLAGS(GTK_WIDGET(toolItem), GTK_CAN_FOCUS);   /*取消默认锁定*/
    gtk_toolbar_insert(GTK_TOOLBAR(toolbar->toolbar), toolItem, -1);
    return toolItem;
}

void on_colorToolItem_clicked(GtkWidget *widget, gpointer window)
{
    select_color(((MainWindow *)window)->label);
}

void select_color(GtkWidget *label)
{
    GtkResponseType result;
    GtkColorSelection *colorSel;
    GtkWidget *dialog = gtk_color_selection_dialog_new("Font Color");

    result = gtk_dialog_run(GTK_DIALOG(dialog));

    if (GTK_RESPONSE_OK == result) {
        GdkColor color;
        colorSel = GTK_COLOR_SELECTION(GTK_COLOR_SELECTION_DIALOG(dialog)->colorsel);
        gtk_color_selection_get_current_color(colorSel, &color);
        gtk_widget_modify_fg(GTK_WIDGET(label), GTK_STATE_NORMAL, &color);
    }
    gtk_widget_destroy(dialog);
}

void on_fontToolItem_clicked(GtkWidget *widget, gpointer window)
{
    select_font(((MainWindow *)window)->label);
}


void select_font(GtkWidget *label)
{
    GtkResponseType result;
    GtkWidget *fontSelectionDialog = gtk_font_selection_dialog_new("Select Font");
    result = gtk_dialog_run(GTK_DIALOG(fontSelectionDialog));
    if (result == GTK_RESPONSE_OK || result == GTK_RESPONSE_APPLY) {
        PangoFontDescription *font_desc;
        gchar *fontname = gtk_font_selection_dialog_get_font_name(GTK_FONT_SELECTION_DIALOG(fontSelectionDialog));
        font_desc = pango_font_description_from_string(fontname);
        gtk_widget_modify_font(GTK_WIDGET(label), font_desc);
        g_free(fontname);
    }
    gtk_widget_destroy(fontSelectionDialog);
}

void on_fileChooserItem_clicked(GtkWidget *widget, gpointer window)
{
	GtkWidget *dialog = gtk_file_chooser_dialog_new("File Chooser",
			window, GTK_FILE_CHOOSER_ACTION_OPEN,
			("_Cancel"), GTK_RESPONSE_CANCEL,
			("_OK"), GTK_RESPONSE_OK,
			NULL);

	GSList *list = NULL;
	GSList *it = NULL;
	gchar *filename = NULL;

	gtk_file_chooser_set_current_folder (GTK_FILE_CHOOSER(dialog), "/home/jake");
	gtk_file_chooser_add_shortcut_folder(GTK_FILE_CHOOSER(dialog), "/home/jake/bin", NULL);

	list = gtk_file_chooser_list_shortcut_folders(GTK_FILE_CHOOSER(dialog));
	if ( NULL == list ) {
		g_message("get empty");
	}
	it = list;
	for (; it; it=it->next) {
		filename = it->data;
		g_message("filename = %s\n", filename);
	}
	g_slist_free(list);


	if (gtk_dialog_run(GTK_DIALOG(dialog)) == GTK_RESPONSE_OK) {
		gchar *name = gtk_file_chooser_get_filename(GTK_FILE_CHOOSER(dialog));
		g_message("filename = %s\n", name);
		g_free(name);
	}
	gtk_widget_destroy(dialog);
}
